package com.jintian.smart.kernel.orm.controller;

import java.util.List;
import java.util.Map;

import cn.hutool.core.lang.Dict;
import com.jintian.smart.kernel.core.web.RestResultWrapper;
import com.jintian.smart.kernel.orm.query.Query;
import com.jintian.smart.kernel.orm.service.EntityCommonService;
import io.swagger.v3.oas.annotations.Operation;
import org.beetl.sql.core.page.PageResult;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@RestResultWrapper
public abstract class AbstractCommonController {
    protected final EntityCommonService commonService;

    public AbstractCommonController(EntityCommonService commonService) {
        this.commonService = commonService;
    }

    @GetMapping(path = "/get")
    @Operation(summary = "通过id获取唯一实体")
    public Object get(String model, String path, String entity, String id) throws ClassNotFoundException {
        return commonService.getEntityById(model, path, entity, id);
    }

    @PostMapping("/batchSave")
    @Operation(summary = "批量保存")
    public Object batchSave(String model, String path, @RequestBody List<Map<String, Object>> batch)
            throws ClassNotFoundException {
        Object ret = commonService.saveBatchChangeLog(model, path, batch);
        return ret;
    }

    @PostMapping("/query")
    @Operation(summary = "简单查询")
    public PageResult<?> simpleQuery(String model, String path, String entity, @RequestBody Dict request)
            throws ClassNotFoundException {
        PageResult<?> ret = commonService.simpleQuery(model, path, entity, request);
        return ret;
    }

    @PostMapping("/save")
    @Operation(summary = "保存修改记录")
    @Transactional
    public Object saveChangeLog(String model, String path, String entity, @RequestBody Map<String, Object> request)
            throws ClassNotFoundException {
        return this.commonService.saveChangeLog(model, path, entity, request);
    }

    @PostMapping("/create")
    @Operation(summary = "创建实例")
    public List<?> create(String model, String path, String entity, @RequestBody Map<String, Object> request)
            throws ClassNotFoundException {
        return this.commonService.batchCreate(model, path, entity, request);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除数据")
    public boolean delete(String model, String path, String entity, @RequestBody Map<String, Object> request)
            throws ClassNotFoundException {
        return this.commonService.delete(model, path, entity, request);
    }

    @PostMapping("/viewQuery")
    @Operation(summary = "视图自定义查询")
    public PageResult<?> viewQuery(String model, String path, String entity, @RequestBody Query query)
            throws Exception {
        return this.commonService.viewQuery(model, path, entity, query);
    }

    @PostMapping("/viewSimpleQuery")
    @Operation(summary = "视图简单查询")
    public PageResult<?> viewSimpleQuery(String model, String path, @RequestParam String viewId, String simpleQuery,
                                         String entity, @RequestBody Dict request) throws Exception {
        return this.commonService.viewSimpleQuery(model, path, viewId, simpleQuery, entity, request);
    }
}
